import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class MemberLevelService extends BaseService<any> {
  private readonly logger = new Logger(MemberLevelService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: MemberLevelService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[], order: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'level_id,site_id,level_name,growth,remark,create_time,update_time,level_benefits,level_gifts';         $search_model = $this->model->where([...
const field = 'level_id,site_id,level_name,growth,remark,create_time,update_time,level_benefits,level_gifts';
        const search_model = this.model.where([ [ 'site_id', '=', this.site_id ) )).withSearch([ 'level_name' ), where).field(field).append([ "member_num" )).order(order);
        return this.pageQuery(search_model, function (item) {
            if (!!item.level_benefits)) item.level_benefits = CoreMemberService.getBenefitsContent(item.site_id, item.level_benefits);
            if (!!item.level_gifts)) item.level_gifts = CoreMemberService.getGiftContent(item.site_id, item.level_gifts);
  }

  /**
   * getList
   * 对应 PHP: MemberLevelService_admin::getList()
   * 逻辑类型: undefined - undefined
   */
  async getList(where: any[], field: any[]) {
    // 基于PHP真实逻辑: getList
    // PHP原文: $order = 'growth asc';         return $this->model->where([['site_id', '=', $this->site_id]])->withSearch([ "level_name" ], $where)->field($field)->or...
const order = 'growth asc';
        return this.model.where([['site_id', '=', this.site_id))).withSearch([ "level_name" ), where).field(field).order(order).select().toArray(];
    }

    /**
     * 获取会员等级信息
     * @param int level_id
     * @return */
    async getInfo(int level_id)
    {
        const field = 'level_id,site_id,level_name,growth,remark,create_time,update_time,level_benefits,level_gifts';
        return this.model.field(field).where([ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )).findOrEmpty().toArray(];
    }

    /**
     * 获取等级
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getAll()
    {
        return ( this.coreMemberLevelService ).getAll(this.site_id);
    }

    /**
     * 添加会员等级
     * @param data
     * @return mixed
     */
    async add(data)
    {
        const count = this.model.where([ ['site_id', '=', this.site_id ) )).count(];
        if (count >= 10) throw new BusinessException('MEMBER_LEVEL_MAX'];

        data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return res.level_id;
    }

    /**
     * 会员等级编辑
     * @param int level_id
     * @param data
     * @return true
     */
    async edit(int level_id, data)
    {
        data[ 'update_time' ] = time(];
        this.model.update(data, [ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )];
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return true;
    }

    /**
     * 删除会员等级
     * @param int level_id
     * @return bool
     */
    async del(int level_id)
    {
        const level = this.model.where([ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )).append(.member_num).findOrEmpty(];
        if (level.member_num > 0) throw new BusinessException('LEVEL_NOT_ALLOWED_DELETE'];
        level.delete();
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return true;
    }

}
  }

  /**
   * getInfo
   * 对应 PHP: MemberLevelService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(level_id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'level_id,site_id,level_name,growth,remark,create_time,update_time,level_benefits,level_gifts';         return $this->model->field($field)->w...
const field = 'level_id,site_id,level_name,growth,remark,create_time,update_time,level_benefits,level_gifts';
        return this.model.field(field).where([ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )).findOrEmpty().toArray(];
    }

    /**
     * 获取等级
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getAll()
    {
        return ( this.coreMemberLevelService ).getAll(this.site_id);
    }

    /**
     * 添加会员等级
     * @param data
     * @return mixed
     */
    async add(data)
    {
        const count = this.model.where([ ['site_id', '=', this.site_id ) )).count(];
        if (count >= 10) throw new BusinessException('MEMBER_LEVEL_MAX'];

        data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return res.level_id;
    }

    /**
     * 会员等级编辑
     * @param int level_id
     * @param data
     * @return true
     */
    async edit(int level_id, data)
    {
        data[ 'update_time' ] = time(];
        this.model.update(data, [ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )];
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return true;
    }

    /**
     * 删除会员等级
     * @param int level_id
     * @return bool
     */
    async del(int level_id)
    {
        const level = this.model.where([ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )).append(.member_num).findOrEmpty(];
        if (level.member_num > 0) throw new BusinessException('LEVEL_NOT_ALLOWED_DELETE'];
        level.delete();
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return true;
    }

}
  }

  /**
   * getAll
   * 对应 PHP: MemberLevelService_admin::getAll()
   * 逻辑类型: undefined - undefined
   */
  async getAll() {
    // 基于PHP真实逻辑: getAll
    // PHP原文: return ( new CoreMemberLevelService() )->getAll($this->site_id);     }      /**      * 添加会员等级      * @param array $data      * @return mixed      */  ...
return ( this.coreMemberLevelService ).getAll(this.site_id);
    }

    /**
     * 添加会员等级
     * @param data
     * @return mixed
     */
    async add(data)
    {
        const count = this.model.where([ ['site_id', '=', this.site_id ) )).count(];
        if (count >= 10) throw new BusinessException('MEMBER_LEVEL_MAX'];

        data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return res.level_id;
    }

    /**
     * 会员等级编辑
     * @param int level_id
     * @param data
     * @return true
     */
    async edit(int level_id, data)
    {
        data[ 'update_time' ] = time(];
        this.model.update(data, [ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )];
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return true;
    }

    /**
     * 删除会员等级
     * @param int level_id
     * @return bool
     */
    async del(int level_id)
    {
        const level = this.model.where([ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )).append(.member_num).findOrEmpty(];
        if (level.member_num > 0) throw new BusinessException('LEVEL_NOT_ALLOWED_DELETE'];
        level.delete();
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return true;
    }

}
  }

  /**
   * add
   * 对应 PHP: MemberLevelService_admin::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: $count = $this->model->where([ ['site_id', '=', $this->site_id ] ])->count();         if ($count >= 10) throw new CommonException('MEMBER_LEVEL_MAX');...
const count = this.model.where([ ['site_id', '=', this.site_id ) )).count(];
        if (count >= 10) throw new BusinessException('MEMBER_LEVEL_MAX'];

        data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return res.level_id;
    }

    /**
     * 会员等级编辑
     * @param int level_id
     * @param data
     * @return true
     */
    async edit(int level_id, data)
    {
        data[ 'update_time' ] = time(];
        this.model.update(data, [ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )];
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return true;
    }

    /**
     * 删除会员等级
     * @param int level_id
     * @return bool
     */
    async del(int level_id)
    {
        const level = this.model.where([ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )).append(.member_num).findOrEmpty(];
        if (level.member_num > 0) throw new BusinessException('LEVEL_NOT_ALLOWED_DELETE'];
        level.delete();
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return true;
    }

}
  }

  /**
   * edit
   * 对应 PHP: MemberLevelService_admin::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(level_id: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: $data[ 'update_time' ] = time();         $this->model->update($data, [ [ 'level_id', '=', $level_id ], [ 'site_id', '=', $this->site_id ] ]);         ...
data[ 'update_time' ] = time(];
        this.model.update(data, [ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )];
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return true;
    }

    /**
     * 删除会员等级
     * @param int level_id
     * @return bool
     */
    async del(int level_id)
    {
        const level = this.model.where([ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )).append(.member_num).findOrEmpty(];
        if (level.member_num > 0) throw new BusinessException('LEVEL_NOT_ALLOWED_DELETE'];
        level.delete();
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return true;
    }

}
  }

  /**
   * del
   * 对应 PHP: MemberLevelService_admin::del()
   * 逻辑类型: undefined - undefined
   */
  async del(level_id: number) {
    // 基于PHP真实逻辑: del
    // PHP原文: $level = $this->model->where([ [ 'level_id', '=', $level_id ], [ 'site_id', '=', $this->site_id ] ])->append(['member_num'])->findOrEmpty();         i...
const level = this.model.where([ [ 'level_id', '=', level_id ], [ 'site_id', '=', this.site_id ) )).append(.member_num).findOrEmpty(];
        if (level.member_num > 0) throw new BusinessException('LEVEL_NOT_ALLOWED_DELETE'];
        level.delete();
        ( this.coreMemberLevelService ).clearCache(this.site_id);
        return true;
    }

}
  }
}
